package com.bye.xiaomi_mall.service.impl;

import java.util.List;

import com.bye.xiaomi_mall.domain.TdOrderDetail;
import com.bye.xiaomi_mall.mapper.TdOrderDetailMapper;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bye.xiaomi_mall.mapper.TdOrderMapper;
import com.bye.xiaomi_mall.domain.TdOrder;
import com.bye.xiaomi_mall.service.ITdOrderService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
/**
 * 订单Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-15
 */
@Service
public class TdOrderServiceImpl implements ITdOrderService {
    @Autowired
    private TdOrderMapper tdOrderMapper;
    @Autowired
    private TdOrderDetailMapper detailMapper;

    /**
     * 查询订单
     *
     * @param id 订单主键
     * @return 订单
     */
    @Override
    public TdOrder selectTdOrderById(Long id) {
        return tdOrderMapper.selectTdOrderById(id);
    }

    /**
     * 查询订单列表
     *
     * @param tdOrder 订单
     * @return 订单
     */
    @Override
    public List<TdOrder> selectTdOrderList(TdOrder tdOrder) {
        return tdOrderMapper.selectTdOrderList(tdOrder);
    }

    /**
     * 新增订单
     *
     * @param tdOrder 订单
     * @return 结果
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int insertTdOrder(TdOrder tdOrder) {
        List<TdOrderDetail> orderDetailList = tdOrder.getOrderDetailList();

        for (TdOrderDetail orderDetail : orderDetailList) {
            orderDetail.setOrderCode(tdOrder.getOrderCode());
//            添加订单详情表
            detailMapper.insertTdOrderDetail(orderDetail);
        }


//        添加订单表
        return tdOrderMapper.insertTdOrder(tdOrder);
    }

    /**
     * 修改订单
     *
     * @param tdOrder 订单
     * @return 结果
     */
    @Override
    public int updateTdOrder(TdOrder tdOrder) {
        return tdOrderMapper.updateTdOrder(tdOrder);
    }

    /**
     * 批量删除订单
     *
     * @param ids 需要删除的订单主键
     * @return 结果
     */
    @Override
    public int deleteTdOrderByIds(Long[] ids) {
        return tdOrderMapper.deleteTdOrderByIds(ids);
    }

    /**
     * 删除订单信息
     *
     * @param id 订单主键
     * @return 结果
     */
    @Override
    public int deleteTdOrderById(Long id) {
        return tdOrderMapper.deleteTdOrderById(id);
    }

    @Override
    public TdOrder selectOrderInfoByOrderCode(String orderCode) {
        return tdOrderMapper.selectTdOrderByOrderCode(orderCode);
    }

    @Override
    public List<TdOrder> selectOrderInfoByUserId(String userID) {
        List<TdOrder> tdOrders = tdOrderMapper.selectOrderInfoByUserId(userID);
        for(TdOrder order:tdOrders){
            List<TdOrderDetail> orderDetailList = detailMapper.selectOrderDetail(order.getOrderCode());
            if (null!=orderDetailList){
                order.setOrderDetailList(orderDetailList);
            }
        }
        return tdOrders;
    }

}
